/*
 *
 * Copyright 2011 and 2012 University of Illinois
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

var EXPORTED_SYMBOLS = ["OAA_CI_UTILS"];

/**
 * @namespace OAA_CI_UTILS
 */

var OAA_CI_UTILS = {
  result:                null,
  ruleset_nls:           null,
  log:                   null,
  default_toolbarbutton: 'IMAGES_BUTTON',
  default_menuitem:      'ID_MENUITEM_IMAGES',
  last_toolbarbutton_id: "",
  last_menuitem_id:      "",
  id:                    "",
  last_node_highlighted: null,
  console_enabled:       true
}


OAA_CI_UTILS.consoleService = (typeof Components !== "undefined") ?
                      Components.classes["@mozilla.org/consoleservice;1"].getService(
                      Components.interfaces.nsIConsoleService) : null;

  // private methods
OAA_CI_UTILS.logToConsole = function (message) {
    // use JS Shell print function if not in Firefox
    if (OAA_CI_UTILS.consoleService)
      OAA_CI_UTILS.consoleService.logStringMessage(message);
    else
      print(message);
};


/**
 * @function setStateToolbarAndMenus
 *
 * @memberOf OAA_CI_UTILS
 *
 * @desc Set the checked state of the toolbar button associated
 *       with the current sidebar, or turn off if no sidebar is
 *       displayed. Also keep the checked state of the menuitem
 *       on the View/Sidebar menu in sync with sidebar display.
 */

OAA_CI_UTILS.setStateToolbarAndMenus = function (window, toolbarbutton_id, menuitem_id) {

  if (!window) return;

  var last_toolbarbutton = null;
  
  if (typeof window.oaa_sidebar_last_toolbarbutton_id === 'string' && 
      window.oaa_sidebar_last_toolbarbutton_id.length) {
//    window.logToConsole("get last element by this id: " + window.oaa_sidebar_last_toolbarbutton_id);
    last_toolbarbutton = window.document.getElementById(window.oaa_sidebar_last_toolbarbutton_id);
  }  
  
  var toolbarbutton = null;
  if (typeof toolbarbutton_id === 'string' && 
      toolbarbutton_id.length) {
//    window.logToConsole("get new element by this id: " + toolbarbutton_id);
    toolbarbutton = window.document.getElementById(toolbarbutton_id);
  }  

  OAA_CI_UTILS.logToConsole("last toolbar button: " + window.oaa_sidebar_last_toolbarbutton_id + " ( " + last_toolbarbutton + ")  new toolbar button " + toolbarbutton_id + " (" + toolbarbutton + ")");

  if (last_toolbarbutton) last_toolbarbutton.checked = false;
  
  if (toolbarbutton) { 
    toolbarbutton.checked = true;
    window.oaa_sidebar_last_toolbarbutton_id = toolbarbutton_id;
  }

  var last_menuitem = null;
  if (typeof window.oaa_sidebar_last_menuitem_id === 'string' &&
      window.oaa_sidebar_last_menuitem_id.length) {
    last_menuitem = window.document.getElementById(window.oaa_sidebar_last_menuitem_id);
  }

  var menuitem = null;
  if (typeof menuitem_id === 'string' &&
      menuitem_id.length) {
    menuitem = window.document.getElementById(menuitem_id);
  }

  OAA_CI_UTILS.logToConsole("last menuitem: " + window.oaa_sidebar_last_menuitem_id + " ( " + last_menuitem + ") new menuitem" + menuitem_id + " (" + menuitem+ ")");

  if (last_menuitem) last_menuitem.setAttribute('checked', 'false');
  
  if (menuitem) { 
    menuitem.setAttribute('checked', 'true');
    window.oaa_sidebar_last_menuitem_id = menuitem_id;
  }

};

